package com.example.rabbitmqdemo.TtlConfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author：DijaH
 * @Date：2023/12/7 14:44
 * @Version：1.0.0
 * @Description：
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SenMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable("message") String message){
        log.info("发送一条TTL消息：{}",message);
        rabbitTemplate.convertAndSend("X","XA","10s的队列"+message);
        rabbitTemplate.convertAndSend("X","XB","40s的队列"+message);
    }

    @GetMapping("/ttl/send/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message, @PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", message, correlationData -> {
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(), ttlTime, message);
    }
}
